//
//  UIButton.swift
//  Documentation
//
//  Created by Toj on 2/7/23.
//

import Foundation

@available(iOS 13.4, *)
public typealias UIButtonPointerStyleProvider = (UIButton, __UIPointerEffect, __UIPointerShape) -> UIPointerStyle?

// MARK: - Class
@available(iOS 2.0, *)
open class UIButton : UIControl, NSCoding {

    // MARK: 构造器
    public init(frame: CGRect)
    public init?(coder: NSCoder)
    /// Initializes a custom button, registers primaryAction for the UIControlEventPrimaryActionTriggered control event, and uses primaryAction's title & image as the button's title & image.
    @available(iOS 14.0, *)
    public convenience init(frame: CGRect, primaryAction: UIAction?)
    public convenience init(type buttonType: UIButton.ButtonType)

    // MARK: 构造器
    @available(iOS 13.0, *)
    open class func systemButton(with image: UIImage, target: Any?, action: Selector?) -> Self

    
    /// Requests the view update its configuration for its current state. This method is called automatically when the button's state may have changed, as well as in other circumstances where an update may be required. Multiple requests may be coalesced into a single update at the appropriate time.
    @available(iOS 15.0, *)
    open func setNeedsUpdateConfiguration()

    
    /// Subclasses should override this method and update the button's `configuration`. This method should not be called directly, use `setNeedsUpdateConfiguration` to request an update.
    @available(iOS 15.0, *)
    open func updateConfiguration()

    
    /// Block-based equivalent to overriding -updateConfiguration in a subclass. Setting this handler will force the button into configuration-based behavior (see the `configuration` property). This block is called after `-updateConfiguration`
    @available(iOS 15.0, *)
    open var configurationUpdateHandler: UIButton.ConfigurationUpdateHandler?

    
    /// When YES, the button will automatically call -updatedConfigurationForButton: on its `configuration ` when the button's state changes, and apply the updated configuration to the button. The default value is YES.
    @available(iOS 15.0, *)
    open var automaticallyUpdatesConfiguration: Bool

    
    @available(iOS 5.0, *)
    // 应用到按钮标题和图片上的底色
    open var tintColor: UIColor! // The tintColor is inherited through the superview hierarchy. See UIView for more information.

    // 按钮类型
    open var buttonType: UIButton.ButtonType { get }

    
    /// If pointer effects are enabled for the button, this will return true when an effect is active.
    @available(iOS 15.0, *)
    open var isHovered: Bool { get }

    
    /// Returns true while the button is presenting a menu.
    @available(iOS 15.0, *)
    open var isHeld: Bool { get }

    
    @available(iOS 14.0, *)
    open var role: UIButton.Role // default is UIButtonRoleNormal.

    
    /// Enables this button's built-in pointer interaction.
    @available(iOS 13.4, *)
    open var isPointerInteractionEnabled: Bool

    
    /// An optional menu for the button to display. The button will automatically enable or disable its contextMenuInteraction when a non-nil or nil menu is set. Defaults to nil.
    @available(iOS 14.0, *)
    @NSCopying open var menu: UIMenu?

    
    /// Indicates if the button changes selection as its primary action.
    /// This shows the menu as options for selection if a menu is populated and showsMenuAsPrimaryAction is enabled.
    /// If no menu is provided or it is not the primary action, UIControlStateSelected is toggled on and off for the primary action.
    @available(iOS 15.0, *)
    open var changesSelectionAsPrimaryAction: Bool

    
    // you can set the image, title color, title shadow color, and background image to use for each state. you can specify data
    // for a combined state by using the flags added together. in general, you should specify a value for the normal state to be used
    // by other states which don't have a custom value set
    // 为特定状态设置标题
    open func setTitle(_ title: String?, for state: UIControl.State) // default is nil. title is assumed to be single line
    
    // 为特定状态设置标题颜色
    open func setTitleColor(_ color: UIColor?, for state: UIControl.State) // default is nil. use opaque white
    
    // 为特定状态设置阴影颜色
    open func setTitleShadowColor(_ color: UIColor?, for state: UIControl.State) // default is nil. use 50% black
    
    // 为特定状态设置图片
    open func setImage(_ image: UIImage?, for state: UIControl.State) // default is nil. should be same size if different for different states
    
    // 设置 特定状态: 背景图片
    open func setBackgroundImage(_ image: UIImage?, for state: UIControl.State) // default is nil

    @available(iOS 13.0, *)
    open func setPreferredSymbolConfiguration(_ configuration: UIImage.SymbolConfiguration?, forImageIn state: UIControl.State)

    @available(iOS 6.0, *)
    // 设置 特定状态: 富文本标题
    open func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State) // default is nil. title is assumed to be single line

    // 获取 特定状态: 标题
    open func title(for state: UIControl.State) -> String? // these getters only take a single state value

    // 获取 特定状态: 标题颜色
    open func titleColor(for state: UIControl.State) -> UIColor?
    // 获取 特定状态: 阴影颜色
    open func titleShadowColor(for state: UIControl.State) -> UIColor?

    // 获取 特定状态: 图片
    open func image(for state: UIControl.State) -> UIImage?

    // 获取 特定状态: 背景图片
    open func backgroundImage(for state: UIControl.State) -> UIImage?

    @available(iOS 13.0, *)
    open func preferredSymbolConfigurationForImage(in state: UIControl.State) -> UIImage.SymbolConfiguration?

    @available(iOS 6.0, *)
    // 获取 特定状态: 富文本内容
    open func attributedTitle(for state: UIControl.State) -> NSAttributedString?

    
    // these are the values that will be used for the current state. you can also use these for overrides. a heuristic will be used to
    // determine what image to choose based on the explicit states set. For example, the 'normal' state value will be used for all states
    // that don't have their own image defined.
    
    // MARK: 当前状态内容
    // 当前状态: 标题
    open var currentTitle: String? { get } // normal/highlighted/selected/disabled. can return nil

    // 当前状态: 标题颜色
    open var currentTitleColor: UIColor { get } // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)
//
    open var currentTitleShadowColor: UIColor? { get } // normal/highlighted/selected/disabled.
    
    // 当前状态: 图片
    open var currentImage: UIImage? { get } // normal/highlighted/selected/disabled. can return nil
    
    // 当前状态: 背景图片
    open var currentBackgroundImage: UIImage? { get } // normal/highlighted/selected/disabled. can return nil

    @available(iOS 13.0, *)
    open var currentPreferredSymbolConfiguration: UIImage.SymbolConfiguration? { get } // normal/highlighted/selected/disabled. can return nil

    @available(iOS 6.0, *)
    // 当前状态: 态富文本内容
    open var currentAttributedTitle: NSAttributedString? { get } // normal/highlighted/selected/disabled. can return nil

    // MARK: 懒加载内部UI
    // 标题: 设置文案的时候才会创建
    @available(iOS 3.0, *)
    open var titleLabel: UILabel? { get }

    // 图片: 设置图片的时候才会创建
    @available(iOS 3.0, *)
    open var imageView: UIImageView? { get }

    // 子标题: 设置子标题才会创建, 未使用过
    @available(iOS 15.0, *)
    open var subtitleLabel: UILabel? { get }
}

@available(iOS 13.4, *)
extension UIButton {

    public typealias PointerStyleProvider = (_ button: UIButton, _ proposedEffect: UIPointerEffect, _ proposedShape: UIPointerShape) -> UIPointerStyle?

    public var pointerStyleProvider: UIButton.PointerStyleProvider?
}

extension UIButton {

    @available(iOS 14.0, *)
    public convenience init(type buttonType: UIButton.ButtonType = .system, primaryAction: UIAction?)
}

extension UIButton {

    // The effect of these properties can be replicated via UIButtonConfiguration.contentInset and UIButtonConfiguration.imageToTitlePadding. They are ignored when a configuration is set.
    // 内容偏移
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration")
    open var contentEdgeInsets: UIEdgeInsets /*UIButtonDeprecated*/ // default is UIEdgeInsetsZero. On tvOS 10 or later, default is nonzero except for custom buttons.

    // 标题偏移
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration")
    open var titleEdgeInsets: UIEdgeInsets // default is UIEdgeInsetsZero

    // 图片偏移
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration")
    open var imageEdgeInsets: UIEdgeInsets // default is UIEdgeInsetsZero

    
    // The effect of these properties can be replicated by providing an appropriate UIButtonConfiguration. They are ignored when a configuration set.
    // 当按钮高亮时，确定是否改变标题阴影
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration, you may customize to replicate this behavior via a configurationUpdateHandler")
    open var reversesTitleShadowWhenHighlighted: Bool // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance
    
    // 高亮状态是否使用系统默认状态. 说白了就是禁用系统highlighted样式效果
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration, you may customize to replicate this behavior via a configurationUpdateHandler")
    open var adjustsImageWhenHighlighted: Bool = true
     
    // 不可用状态是否使用系统默认状态. 说白了就是禁用系统disabled样式效果
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration, you may customize to replicate this behavior via a configurationUpdateHandler")
    open var adjustsImageWhenDisabled: Bool = true

    
    // 确定点击按钮时是否发光
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "This property is ignored when using UIButtonConfiguration")
    open var showsTouchWhenHighlighted: Bool

    
    // 返回接收者绘制背景的矩形
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "Override layoutSubviews, call super, and position views as you desire.")
    open func backgroundRect(forBounds bounds: CGRect) -> CGRect

    // 返回接收者绘制整个内容的矩形
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "Override layoutSubviews, call super, and position views as you desire.")
    open func contentRect(forBounds bounds: CGRect) -> CGRect

    // 返回接收者绘制标题的矩形
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "Override layoutSubviews, call super, and position views as you desire.")
    open func titleRect(forContentRect contentRect: CGRect) -> CGRect

    // 返回接收者绘制图片的矩.
    @available(iOS, introduced: 2.0, deprecated: 15.0, message: "Override layoutSubviews, call super, and position views as you desire.")
    open func imageRect(forContentRect contentRect: CGRect) -> CGRect
}

extension UIButton : UISpringLoadedInteractionSupporting {
}
